//
//  Regist2ViewController.m
//  Yu01
//
//  Created by 蒋松涵 on 16/8/19.
//  Copyright © 2016年 蒋松涵. All rights reserved.
//

#import "Regist2ViewController.h"
#import "ViewController.h"

@interface Regist2ViewController ()
{
    UIView *bgView;
    UITextField *passward;
}

@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSMutableDictionary *urlParameters;

//宏定义NSUserDefaults
#define APP_DEF [NSUserDefaults standardUserDefaults]
@end

@implementation Regist2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _urlStr =  @"http://www.100xsys.com.cn/school/app/commonAccoutInfo/updatePwd";
    _urlParameters = [[NSMutableDictionary alloc]init];
    [_urlParameters setObject:@"xuesheng1" forKey:@"accoutInfo.id"];
    [_urlParameters setObject:@"123456789" forKey:@"accoutInfo.password"];


    self.title=@"重设密码";
    self.view.backgroundColor=[UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
//    [self fecthDataFromNet:_urlStr parameters:_urlParameters];
    [self post];
    //创建textFields
    [self createTextFields];
}

-(void)post
{
    
    //http://www.100xsys.com.cn/school/app/signInfo/getInfoJson
    
    
    //创建会话对象
    NSURLSession *session = [NSURLSession sharedSession];
    
    
    //根据会话对象创建task
    NSURL *url = [NSURL URLWithString:@"http://www.100xsys.com.cn/school/app/commonAccoutInfo/updatePwd"];
    

    
    //创建可变的请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    //修改请求方法为POST
    request.HTTPMethod = @"POST";
    
    //设置请求体
    request.HTTPBody = [@"accoutInfo.id=xuesheng1&accoutInfo.password=123456789" dataUsingEncoding:NSUTF8StringEncoding];
    
    //设置请求体
    //        NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@",];
    //把拼接后的字符串转换为data，设置请求体
    //        request.HTTPBody=[param dataUsingEncoding:NSUTF÷8StringEncoding];
    
    //根据会话对象创建一个Task(发送请求）
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        //8.解析数据
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"%@",dict);
        
        
    }];
    
    //7.执行任务
    [dataTask resume];
}


//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSMutableDictionary *)parameter {
    
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", nil];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
//        NSLog(@"登录-%@",dic[@"message"]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
    
}


//创建textFields
-(void)createTextFields
{
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(30, 75, self.view.frame.size.width-90, 30)];
    label.text=@"请设置密码";
    label.textColor=[UIColor grayColor];
    label.textAlignment=UITextAlignmentLeft;
    label.font=[UIFont systemFontOfSize:13];
    [self.view addSubview:label];
    
    CGRect frame=[UIScreen mainScreen].bounds;
    bgView=[[UIView alloc]initWithFrame:CGRectMake(10, 110, frame.size.width-20, 50)];
    bgView.layer.cornerRadius=3.0;
    bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgView];
    
    passward = [[UITextField alloc]initWithFrame:CGRectMake(100, 10, 200, 30)];
    passward.placeholder = @"6-20位字母或数字";
    passward.font = [UIFont systemFontOfSize:14];
    passward.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    passward.secureTextEntry=YES;
    [bgView addSubview:passward];
    
    UILabel *phonelabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 12, 50, 25)];
    phonelabel.text=@"密码";
    phonelabel.textColor=[UIColor blackColor];
    phonelabel.textAlignment=UITextAlignmentLeft;
    phonelabel.font=[UIFont systemFontOfSize:14];
    [bgView addSubview:phonelabel];
    
    UIButton * nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.frame = CGRectMake(10, bgView.frame.size.height+bgView.frame.origin.y+30,self.view.frame.size.width-20, 37);
    nextBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.backgroundColor=[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1];
    nextBtn.layer.cornerRadius=5.0f;
    [self.view addSubview:nextBtn];
}

//下一步
-(void)nextClick
{
    [APP_DEF setObject:passward.text forKey:@"userpassward"];
    [APP_DEF synchronize];
    
    ViewController * vc = [[ViewController alloc]init];

    [self presentViewController:vc animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
